package com.yjt.fresh.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.HttpManager;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.listener.HttpOnNextListener;
import com.yjt.fresh.R;
import com.yjt.fresh.adpater.AdpRvProductsCategoryContent;
import com.yjt.fresh.adpater.AdpRvProductsCategoryMenu;
import com.yjt.fresh.api.ApiGetAllCategories;
import com.yjt.fresh.base.BaseSimpleActivity;
import com.yjt.fresh.bean.BeanGoodsCategories;
import com.yjt.fresh.constant.MyConstants;
import com.yjt.fresh.utils.CommonsToolsHelper;
import com.yjt.fresh.widget.SpaceItemDecorationHorizontal;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 产品分类
 * Created by mcc on 2018/6/15.
 */

public class AtyProductCategory extends BaseSimpleActivity {
    @BindView(R.id.rv_app_aty_category_menu)
    RecyclerView mRvMenu;
    @BindView(R.id.rv_app_aty_category_content)
    RecyclerView mRvContent;
    @BindView(R.id.rl_app_aty_category_back)
    RelativeLayout mRlBack;
    @BindView(R.id.tv_app_aty_category_search)
    TextView mTvSearch;
    @BindView(R.id.btn_app_aty_category_search)
    Button mBtnSearch;
    @BindView(R.id.srlAppAtyCategory)
    SwipeRefreshLayout mRefreshLayout;
    @BindView(R.id.tvAppAtyCategoryTitle)
    TextView mTvTitle;
    @BindView(R.id.rlAppAtyCategoryAllProduct)
    RelativeLayout mRlAllProduct;
    @BindView(R.id.rl_app_aty_category_search)
    RelativeLayout mRlSearch;
    private AdpRvProductsCategoryMenu mAdpMenu;
    private AdpRvProductsCategoryContent mAdpContent;
    private int mFromWhere;
    private String mStoreID;

    @Override
    protected int getContentView() {
        return R.layout.app_aty_category;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        mFromWhere = getIntent().getIntExtra(MyConstants.FROM_WHERE,0);
        if (mFromWhere == MyConstants.STORE){
            mRlAllProduct.setVisibility(View.VISIBLE);
            mRlSearch.setVisibility(View.GONE);
            mBtnSearch.setVisibility(View.GONE);
            mTvTitle.setVisibility(View.VISIBLE);
            mStoreID = getIntent().getStringExtra(MyConstants.STORE_ID);
        }
    }

    @Override
    protected void initEvent() {
        initRvMenu();
        initRvContent();
        initClick();
        initRefresh();
        getData();
    }


    private void initRvMenu() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        mRvMenu.setLayoutManager(layoutManager);
        List<BeanGoodsCategories> list = new ArrayList<>();
        mAdpMenu = new AdpRvProductsCategoryMenu(R.layout.app_recyle_item_category_menu, list);
        mRvMenu.setAdapter(mAdpMenu);
    }

    private void initRvContent() {
        GridLayoutManager layoutManager = new GridLayoutManager(mContext, 2);
        mRvContent.setLayoutManager(layoutManager);
        List<BeanGoodsCategories.SecondtypeListBean> list = new ArrayList<>();

        int space = CommonsToolsHelper.getWindowWidth(mContext) - CommonsToolsHelper.dip2px(mContext, 70) * 2 - CommonsToolsHelper.dip2px(mContext, 200);
        mRvContent.addItemDecoration(new SpaceItemDecorationHorizontal(space, 2));
        mAdpContent = new AdpRvProductsCategoryContent(R.layout.app_recycle_item_category_content, list);
        mRvContent.setAdapter(mAdpContent);
    }

    private void initClick() {
        mRlAllProduct.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mAdpMenu.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for (int i = 0; i < mRvMenu.getChildCount(); i++) {
                    if (i != position){
                        TextView v = (TextView) mRvMenu.getChildAt(i);
                        v.setBackgroundColor(getResources().getColor(R.color.color_F5F5F5));
                        v.setTextColor(getResources().getColor(R.color.color_333333));
                    }
                }
                TextView textView = (TextView) view;
                textView.setBackgroundColor(getResources().getColor(R.color.color_43b624));
                textView.setTextColor(getResources().getColor(R.color.white));
                mAdpContent.setNewData(mAdpMenu.getItem(position).getSecondtypeList());
            }
        });

        mAdpContent.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(mContext, AtyProducts.class);
                intent.putExtra(MyConstants.SEARCH_OR_CATEGORY, MyConstants.CATEGORY);
                intent.putExtra(MyConstants.PRODUCT_FIRST_TYPE, mAdpContent.getItem(position).getGOODS_FIRST_TYPE());
                intent.putExtra(MyConstants.PRODUCT_SECOND_TYPE, mAdpContent.getItem(position).getTITLE());
                if (mStoreID != null) {
                    intent.putExtra(MyConstants.STORE_ID,mStoreID);
                }
                startActivity(intent);
            }
        });

        mRlBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mTvSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, AtySearch.class);
                startActivity(intent);
            }
        });

        mBtnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, AtySearch.class);
                startActivity(intent);
            }
        });
    }


    private void initRefresh() {
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData();
                mAdpMenu.notifyDataSetChanged();
            }
        });
    }

    HttpOnNextListener<List<BeanGoodsCategories>> mCategoriesListener = new HttpOnNextListener<List<BeanGoodsCategories>>() {
        @Override
        public void onNext(List<BeanGoodsCategories> beanGoodsCategories) {
            mAdpMenu.setNewData(beanGoodsCategories);
            mAdpContent.setNewData(beanGoodsCategories.get(0).getSecondtypeList());
            mRefreshLayout.setRefreshing(false);
        }

        @Override
        public void onReLogin() {
            toLogin();
        }
    };

    private void getData() {
        ApiGetAllCategories apiGetAllCategories = new ApiGetAllCategories(mCategoriesListener, mContext, true, false, true);
        Map<String, Object> map = new HashMap<>();
        if (mFromWhere != MyConstants.STORE) {
            map.put("POSITION", 0);
        }
        apiGetAllCategories.setMap(map);
        HttpManager.getInstance().doHttpDeal(apiGetAllCategories);
    }

}
